/* 用户模块的vuex操作 */

const state = {
  // 从localStorage中获取
  token: JSON.parse(localStorage.getItem('hm-news-token')) || {}
}

const mutations = {
  setToken(state, payload) {
    state.token = payload

    // 把payload保存到localStorage中
    // localStorage中如果要存储复杂类型的数据，需要先转成JSON字符串进行存储。
    localStorage.setItem('hm-news-token', JSON.stringify(payload))
  },
  removeToken(state) {
    state.token = {}

    localStorage.removeItem('hm-news-token')
  }
}

const actions = {}
const getters = {}

export default {
  //   namespaced: true,
  //   state: {
  //     // token: JSON.parse(localStorage.getItem('hm-news-token')) || {}
  //   },
  //   mutations: {
  //     setToken(state, payload) {
  //       state.token = payload

  //       // 把token数据给本地存储起来
  //       localStorage.setItem('hm-news-token', JSON.stringify(payload))
  //     }
  //   },
  //   actions: {}
  state,
  mutations,
  actions,
  getters,
  // 开启命名空间
  namespaced: true
}
